<script setup lang='ts'>
import { getClassAll, type IClass } from '@/api/classApi';
import { batchAddStudent } from '@/api/userApi';
import { ElMessage } from 'element-plus';
import { ref } from 'vue';

defineOptions({
  name: 'VBatchAdd',
})

const classList = ref<IClass[]>([])
async function fetchClassAll() {
  const result = await getClassAll()
  classList.value = result.data as IClass[]
}

fetchClassAll()

const classId = ref<number | null>(null)
const handleSelectChange = (value: number) => {
  classId.value = value
}

const fileDom = ref<HTMLInputElement | null>(null)

const handleInput = async (e: Event) => {
  console.log(fileDom.value?.files?.[0]);
  const file = fileDom.value?.files?.[0]
  const name = file?.name
  const filenameParts = name?.split(".")
  if (filenameParts[filenameParts?.length - 1] !== "xlsx") {
    // 文件格式错误
    ElMessage.error("文件格式错误, 请上传xlsx文件")
    return
  }

  // 上传文件
  const { code, msg } = await batchAddStudent(file, classId.value)
  if (code === 2000) {
    ElMessage.success("批量导入成功")
  } else {
    fileDom.value!.value = null
    ElMessage.error(msg)
  }

}

</script>

<template>
  <div class='container'>
    <div class="operator">
      <el-select @change="handleSelectChange" v-model="classId" placeholder="选择班级" size="large" style="width: 240px">
        <el-option v-for="item in classList" :key="item.id" :label="item.nickname" :value="item.id" />
      </el-select>
    </div>
    <div class="upload-area" v-if="classId">
      <input class="uploader" type="file" ref="fileDom" @input="handleInput" />
      <p class="">
        <el-icon size="40" color="#555">
          <CirclePlus />
        </el-icon>
      </p>

      <!-- <p v-if="isExpired" class="el-icon-lock warn"></p> -->
      <!-- <p v-else :class="isSelectFile ? 'el-icon-upload' : 'el-icon-circle-plus'"
        :style="{ color: isSelectFile ? '#409eff' : '#000' }"></p>
      <p v-if="isExpired" class="el-icon-lock warn"></p> -->
    </div>
  </div>
</template>

<style scoped lang='scss'>
@import "@/assets/mixin.scss";

.upload-area {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  position: relative;
  margin-right: 10px;
  width: 100px;
  height: 100px;
  box-sizing: border-box;
  border: 1px dashed #b6afaf;
  margin-top: 30px;

  p {
    margin: 0;
    position: absolute;
    font-size: 50px;
    // left: 0;
    // top: 0;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

  }
}

.uploader {
  width: 100%;
  height: 100%;
  position: absolute;
  z-index: 1;
  opacity: 0;
  cursor: pointer;
  left: 0;
  top: 0;
}
</style>